<template>
  <div class="detail">
      <Title></Title>
      <div class="detail_box">
         <div class="inner">
           <!-- 购物车上半部分 -->
              <div class="detail_store">
                  <div class="detail_left">
                    <div class="swiper-container" v-if="skudata">
                      <div class="swiper-wrapper">
                        <div class="swiper-slide" v-for="(item,index) in detail.goods_imgs"><img :src="[item]"></div>
                      </div>
                      <!-- 如果需要分页器 -->
                      <div class="swiper-pagination"></div>
                    </div>
                    <div class="skuimg" v-else>
                        <img :src="[detail.goods_img]" v-if="detail.sku[0].spec_show=='单规格'"/>
                        <img :src="[detail.sku[skuid].goods_sku_image]" v-else alt="" srcset="">
                    </div>
                  </div>
                  <div class="detail_right">
                      <div class="detail_tit">
                          {{detail.goods_name}}
                      </div>
                      <div class="detail_price">
                        ￥{{skudata?detail.goods_price:detail.sku[skuid].goods_sku_price}}
                      </div>
                      <div class="detail_mail">
                        <div class="mail_left">
                          运费<span>￥{{detail.logistics_price}}</span>
                        </div>
                        <div class="mail_right">
                          <span >已售 {{detail.goods_virtual_sales}}</span>|<span>剩余 {{skudata?detail.goods_stock:detail.sku[skuid].goods_sku_stock}}</span>
                        </div>
                      </div>
                      <div class="detail_skutit">
                          选择规格
                      </div>
                      <div class="detail_sku">
                        <div v-for="(item,index) in detail.sku" :class="[skuid==index?'skuline':'']" @click="skutabfn(index,item)">
                          <img :src="[detail.goods_img]" v-if="detail.sku[0].spec_show=='单规格'" alt="">
                          <img v-else :src="[item.goods_sku_image]" alt="" srcset="">
                        </div>
                      </div>
                      <div class="detail_num">
                          数量<span @click="add(0)">-</span><span>{{good.car_num}}</span><span @click="add(1)">+</span>
                      </div>
                      <div class="detail_btns">
                          <div class="detail_btn" @click="getorder()">
                            立即购买
                          </div>
                          <div class="detail_btn" @click="addcar()">
                            <img src="../../assets/detail_car.png" alt="" srcset="" >加入购物车
                          </div>
                      </div>
                  </div>
              </div>
              <!-- 商品详情和评价 -->
              <div class="detail_tabs">
                <div class="detail_tab " :class="[detailtabid==0?'detail_line':'']" @click="detailtabfn(0)">
                  商品详情
                </div>
                <div class="detail_tab" :class="[detailtabid==1?'detail_line':'']" @click="detailtabfn(1)">
                  评价
                </div>
              </div>
              <div class="detail_content" v-html="detail.goods_info" v-if="detailtabid==0">
              </div>
              <div class="detail_content" v-if="detailtabid==1">
                <div class="evaluate_up">
                  <div class="evaluate_tab" :class="[evaluateid==0?'evaluate_line':'']" @click="evaluatefn(0)">
                    全部
                  </div>
                  <div class="evaluate_tab" :class="[evaluateid==1?'evaluate_line':'']" @click="evaluatefn(1)">
                    好评
                  </div>
                  <div class="evaluate_tab" :class="[evaluateid==2?'evaluate_line':'']" @click="evaluatefn(2)">
                    差评
                  </div>
                </div>
                <div class="evaluate_none" v-if="evaluatearr.length==0">
                  商品没有评论
                </div>
                <div class="evaluate_list" v-else>
                  <div class="evaluate_item" v-for="(item,index) in evaluatearr">
                      <div class="evaluate_tit">
                          <img :src="[item.avatarUrl]" alt="" srcset="">
                          <div class="evaluate_tit_right">
                            <div class="evaluate_name">
                               {{item.name}}
                            </div>
                            <div class="evaluate_time">
                              {{item.create_time}}
                            </div>
                          </div>
                      </div>
                      <div class="evaluate_info">
                         {{item.content}}
                      </div>

                      <div class="evaluate_img" v-if="item.images!=0" >
                        <img v-for="it in item.images"  :src="[it]" alt="" srcset="">
                      </div>
                  </div>
                  <el-pagination
                        @current-change="handleCurrentChange"
                        :current-page="page"
                        :page-size="page_size"
                        layout="total, prev, pager, next, jumper"
                        :total="total">
                  </el-pagination>
                </div>
              </div>
         </div>
      </div>
  </div>
</template>

<script>
  import Swiper from 'swiper'

  import Title from "../../components/title.vue"
  import {detailapi,editcarapi,getevaluateapi} from "../../api/http.js"
  export default {
    name: 'detail',
    components:{
        Title
    },
    data(){
      return{
        detailtabid:0,//商品详情和评价的切换id
        evaluateid:0,//评价的切换id
        skudata:true,//显示规格
        skuid:0,//规格的切换id选择
        detail:[],
        totalnum:"",//库存
        page:1,
        page_size:5,
        evaluatearr:[],
        total:"",
        good:{
            goods_id:"",//商品的idgoods_id
            car_num:1,//购买的数量car_num
            sku_id:""//规格的idsku_id
        }//立即购买提交的数据
      }
    },
   async created() {
      this.good.goods_id = this.$route.query.goodsid
      const res = await detailapi({goods_id:this.$route.query.goodsid})
      if (res.data.code==200) {
           console.log(res.data.data);

           var array = res.data.data.sku
           for (let i = 0; i < array.length; i++) {
               array[i].spec_show = array[i].spec_show.join("-");
           }
          if (res.data.data.goods_imgs==null) {
               res.data.data.goods_imgs=[]
            }
            res.data.data.goods_imgs.unshift(res.data.data.goods_img)
           this.detail = res.data.data
           this.good.sku_id = array[0].goods_sku_id
           this.totalnum=array[0].goods_sku_stock
            this.$nextTick(() => {
              this._banner()
            })
      } else{
        this.detail=[]
      }
    },
    methods:{
      // 商品详情和评价的切换
     async detailtabfn(star){
        this.detailtabid= star
        if(star==1){
          this._getevaluate({
            goods_id:this.$route.query.goodsid,
            evaluate_type:this.evaluateid,
            page:this.page,
            page_size:this.page_size
          })
        }
      },
      handleCurrentChange(val) {
         this._getevaluate({goods_id:this.$route.query.goodsid,evaluate_type:this.evaluateid,page_size:this.page_size,page:val})
      },
     async _getevaluate({goods_id,evaluate_type,page,page_size}){
        const res = await getevaluateapi({
          goods_id,
          evaluate_type,
          page,
          page_size
        })
        console.log(res.data.data);
        if (res.data.code==200) {
          this.total = res.data.data.total
          this.evaluatearr= res.data.data.data
        } else{
          this.evaluatearr=[]
        }
      },
      // 加入购物车
     async addcar(){
        if (localStorage.getItem('user')==null) {
          this.$message("请先登录")
          this.$router.push({
            path:"/login"
          })
        }
        const res = await editcarapi(this.good)
        if (res.data.code==200) {
          this.$message(res.data.msg)
            this.$router.push({
              path:"/car"
            })
        } else{
          this.$message(res.data.msg)
        }
      },
      // 立即购买
      getorder(){
        if (this.totalnum===0) {
          return this.$message("库存不足")
        }
        var order = {
          goods_sku_id:this.good.sku_id,
          goods_num:this.good.car_num
        }
        this.$router.push({
          path:"/order",
          query: {
            order:JSON.stringify(order)
          }
        })
      },
      // 数量的加减
      add(id){
        if(id==0){
          if(this.good.car_num<=1){
            return this.$message("商品数量不能小于1")
          }
          this.good.car_num--
        }else{
          if(this.good.car_num>=this.totalnum){
           return this.$message("库存不足")
          }
          this.good.car_num++
        }
      },
      // 规格的选择
      skutabfn(index,item){
        console.log(item);
        this.totalnum = item.goods_sku_stock
        this.good.sku_id = item.goods_sku_id
        this.good.car_num = 1
        this.skuid = index
        this.skudata = false
      },
      // 评价的切换
      evaluatefn(star){
        this.page = 1
        this.evaluateid= star
        console.log(star);
        this._getevaluate({
          goods_id:this.$route.query.goodsid,
          evaluate_type:star,
          page:this.page,
          page_size:this.page_size
        })
      },
      _banner() {
        var mySwiper = new Swiper('.detail_left .swiper-container', {
          loop: true,
          autoplay: true, //等同于以下设置
          // 如果需要分页器
          pagination: {
            el: '.swiper-pagination',
          }

        })
      }
    },

  }
</script>

<style lang="less" scoped>
  @import url('swiper/dist/css/swiper.min.css');
  .swiper-container {
    width: 100%;
    height: 100%;
  }

  .swiper-wrapper {
   width: 100%;
   height: 100%;
  }

  .swiper-slide {
    width: 100%;
    height: 100%;
    img {
      width: 100%;
      height: 100%;
    }
  }
.detail_box{
  width: 100%;
  padding-top: 20px;
  .inner{
    width: 1300px;
    margin: 0 auto;
    padding: 0 85px;
    box-sizing: border-box;
    .detail_store{
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 50px;
      .detail_left{
        width: 500px;
        height: 500px;
        img{
          width: 500px;
          height: 500px;
        }
      }
      .detail_right{
        width: 600px;
        height: 500px;
        background: rgba(196, 196, 196, 0.19);
        padding: 30px 15px 0 20px;
        box-sizing: border-box;
          .detail_tit{
            font-size: 18px;
            line-height: 22px;
            color: #292929;
            height: 60px;
            margin-bottom: 25px;
          }
          .detail_price{
            font-weight: 600;
            font-size: 26px;
            line-height: 36px;
            margin-bottom: 20px;
            color: #FF4444;
          }
          .detail_mail{
            width: 100%;
            font-size: 14px;
            color: #9A9A9A;
            display: flex;
            height: 28px;
            align-items: center;
            justify-content: flex-start;
            margin-bottom: 20px;
            .mail_left{
              width: 50%;
              span{
                padding: 0 10px;
              }
            }
            .mail_right{
              width: 50%;
              text-align: right;
              span{
                padding: 0 10px;
              }
            }
          }
          .detail_skutit{
            font-size: 14px;
            height: 28px;
            line-height: 28px;
            color: #9A9A9A;
            margin-bottom: 10px;
          }
          .detail_sku{
            width: 100%;
            margin-bottom: 20px;
            height: 100px;
            display: flex;
            justify-content: flex-start;
            .skuline{
              border: 1px solid #FF4444;
            }
            div{
              width: 40px;
              height: 40px;
              border: 1px solid #fff;
              margin: 5px 8px;
              overflow: hidden;
              img{
                width: 40px;
                height: 40px;
                border: none;
              }
            }

          }
          .detail_num{
            font-weight: 400;
            font-size: 14px;
            color: #9A9A9A;
            height: 40px;
            line-height: 40px;
            margin-bottom: 20px;
              span{
                display: inline-block;
                width: 30px;
                height: 30px;
                line-height: 30px;
                text-align: center;
                background: rgba(225, 227, 229, 0.67);
                border-radius: 2px;
                margin: 0 5px;
                cursor: pointer;
                &:nth-of-type(2){
                  width: 60px;
                }
              }
          }
          .detail_btns{
            width: 100%;
            height: 40px;
            display: flex;
            justify-content: flex-start;

            .detail_btn{
              width: 136px;
              margin-right:20px;
              height: 40px;
              background: #FF4444;
              border-radius: 4px;
              font-size: 16px;
              line-height: 40px;
              color: #FFFFFF;
              text-align: center;
              &:nth-of-type(2){
                img{
                  margin-right: 10px;
                }
                width: 180px;
                background: #555555;
                border-radius: 4px;
              }
            }
          }
      }
    }
    .detail_tabs{
      width: 100%;
      height: 40px;
      .detail_line::after{
        content: "";
        display: block;
        width: 40px;
        height: 2px;
        background-color: #FF4444;
        position: absolute;
        bottom: 0;
        left: 50%;
        margin-left: -20px;
      }
      .detail_tab{
        position: relative;
        font-weight: 400;
        font-size: 16px;
        color: #292929;
        display: inline-block;
        padding: 10px 20px;
        margin: 0 10px;
      }
    }
    .detail_content{
      width: 100%;
      border-top: 1px solid #CCCDCE;
      .evaluate_up{
        width: 100%;
        height: 40px;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        padding-left: 50px;
        box-sizing: border-box;
        margin-bottom: 20px;
        .evaluate_line::after{
          content: "";
          display: block;
          position: absolute;
          width: 70px;
          height: 2px;
          background-color: #FF4444;
          bottom: 0;
          left: 50%;
          margin-left: -35px;
        }
        .evaluate_tab{
          width: 150px;
          position: relative;
          height: 40px;
          line-height: 40px;
          text-align: center;
        }
      }
      .evaluate_none{
        width: 100%;
        height: 200px;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 14px;
        color: #9A9A9A;
      }
      .evaluate_list{
        .evaluate_item{
          width: 100%;
          padding: 0 50px;
          box-sizing: border-box;
          margin-bottom: 15px;
          border-bottom: 1px solid #CCCDCE;
          .evaluate_tit{
            width: 100%;
            height: 60px;
            display: flex;
            justify-content: flex-start;
            align-items: center;
            margin-bottom: 10px;
            img{
              width: 40px;
              height: 40px;
              border-radius: 50%;
              margin-right: 10px;
            }
            .evaluate_tit_right{
              .evaluate_name{
                font-size: 16px;
                color: #292929;
                margin-bottom: 5px;
              }
              .evaluate_time{
                font-size: 16px;
                color: #9A9A9A;
              }
            }
          }
          .evaluate_info{
              font-weight: 400;
              font-size: 16px;
              width: 100%;
              line-height: 22px;
              color: #292929;
              margin-bottom: 10px;
          }
          .evaluate_img{
            width: 100%;
            height: 60px;
            display: flex;
            justify-content: flex-start;
            align-items: center;
            margin-bottom: 10px;
            img{
              width: 60px;
              height: 60px;
              margin: 0 5px;
            }
          }
        }
      }

    }

  }
}
</style>
